

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Vector;



public class RevisionControlParser_Chrome {

	private int revisionID = 0;
	private String logMessage = "";
	private String author = "";
	private String timestamp ;
	private Timestamp timestamp2;
	private String file_list= "";
	private static Connection connect = null;
	private Statement statement = null;
	private static String table = "revisions_chrome";
	///*
	private static String user = "root";
	private static String pass = "123";
    private static String db = "APSEC2014";
    private static String port = "3306";
	//*/
	
	/*
	 private static String user = "sangeetal";
	private static String pass = "sangeetal";
    private static  String db = "APSEC2014";
    private static String port = "3307";
    */
	
	//private Vector<change_file> file_list = new Vector<change_file>();
	
	public void setRevisionID(int revisionID){
		this.revisionID = revisionID;
	}
	
	public int getRevisionID(){
		return revisionID;
	}
	
	public String getLogMessage(){
		return logMessage;
	}
	
	public String getAuthor(){
		return author;
	}
	
	public String getTimeStamp(){
		return timestamp;
	}
	
	public String setFileList(String s)
	{
		file_list = " ";
		//System.out.println("set file change");
		//System.out.println("String="+s);
		int index=s.indexOf("Details");
		int i=s.indexOf("\"Directory\"",index);
		
		int count=0;
		System.out.println("set file change");
	    while(i!=-1)
	    { 
	    	
	    	
	    	int j=s.indexOf("</", i);
	    	String file_name=s.substring(i+14, j);	    	
	    	int k = s.indexOf("\"View Log\">",j);
	    	int l =s.indexOf("</a>",k);
	    	String status = s.substring(k+11, l);
	    	//System.out.println("count="+ count++ +" File name="+file_name+"  status="+status);
	    	file_list = file_list+ status+"+"+file_name+"\n";
	    	
	    	index = i+1;
	    	i=s.indexOf("\"Directory\"",index);
	    	//System.out.println("index="+index+" i="+i+" j="+j+" k="+k+" l="+l);
	    	BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
	    	
			
	    	
	    }
		
		return file_list;
	}
	
	public String getFileList()
	{
		
		return file_list;
	}
	
	public String downloadPage(int revisionID){
		String content = "";
		try {
			//URL url = new URL("http://svn.apache.org/viewvc?view=revision&revision=" + revisionID);
			URL url = new URL("http://src.chromium.org/viewvc/chrome?view=revision&revision=" + revisionID);
			URLConnection conn = url.openConnection();
			conn.setDoOutput(true);
			InputStream in = null;
			in = url.openStream();
			content = pipe(in,"utf-8",revisionID);		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return content;
	}
	/*
	public String downloadPage(int revisionID){
		String content = "";
		try {
			
			BufferedReader br =  new BufferedReader(new FileReader("4.txt"));
			String data;
			while((data = br.readLine())!=null)
			{
				 content =content  + data;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return content;
	}*/
	
	public String pipe(InputStream in,String charset, int revisionID) throws IOException {
		StringBuffer s = new StringBuffer();
		
		if(charset==null||"".equals(charset)){
			charset="utf-8";
		}
		
		String rLine = null;
		BufferedReader bReader = new BufferedReader(new InputStreamReader(in,charset));
		PrintWriter pw = null;
		
		//FileOutputStream fo = new FileOutputStream( revisionID + ".txt");
		//OutputStreamWriter writer = new OutputStreamWriter(fo, "utf-8");
		//pw = new PrintWriter(writer);
		
		while ( (rLine = bReader.readLine()) != null) {
		
			String tmp_rLine = rLine;
			int str_len = tmp_rLine.length();
		
			if (str_len > 0) {
				s.append(tmp_rLine);
				//pw.println(tmp_rLine.replaceAll("\\<.*?>",""));
				//pw.println(tmp_rLine);
				//pw.flush();
			}
	
			tmp_rLine = null;
		}
		
		in.close();	
		//pw.close();
		return s.toString();
		
	}
	
	public void process(){
		String result = "";
		String content = downloadPage(revisionID);
		//String logMessage = "";
		try{
		    String nohtml = content;//content.replaceAll("\\<.*?>","");
		    //System.out.println(nohtml);
		    System.out.println("contnt = "+content);
            content = nohtml;
            
            int beginIndex = content.indexOf("Log Message:");
            int endIndex = content.lastIndexOf("Changed paths:");
            logMessage = content.substring(beginIndex+12, endIndex).trim();
            //System.out.println(logMessage);
            logMessage = logMessage.replaceAll("\\<.*?>","");
            logMessage = logMessage.replaceAll("\'", " ");
            logMessage = logMessage.replaceAll("\"", " ");
            
            System.out.println("contnt = "+content);
            beginIndex = content.indexOf("Author:");
            endIndex = content.indexOf("Date:");
            author = content.substring(beginIndex+7, endIndex).trim().replaceAll("\\<.*?>","");
            System.out.println("author is="+author); 
            beginIndex = content.indexOf("Date:");
            endIndex = content.lastIndexOf("UTC");
            timestamp = content.substring(beginIndex+5, endIndex).trim().replaceAll("\\<.*?>","");
            timestamp = timestamp.substring(4);
            System.out.println("val="+timestamp2);    
            
            //java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //timestamp = sdf.format(timestamp);
            DateFormat formatter = new SimpleDateFormat("MMM dd hh:mm:ss yyyy");
            java.util.Date date = (java.util.Date)formatter.parse(timestamp);
           // Timestamp timets = new Timestamp(date.getTime());
            
           timestamp2= new Timestamp(date.getTime());
           System.out.println("t----- val="+timestamp2);
            
            file_list=setFileList(content);
          
		}catch(Exception e){
			System.out.println();
		}

	}

	//http://src.chromium.org/viewvc/chrome?view=revision&revision=4
	public static void main(String[] args)
	{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		     connect = DriverManager.getConnection("jdbc:mysql://localhost:"+port+"/"+db+"?"
		    		+ "user="+user+"&password="+pass);
		
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		//2519 //105097
		RevisionControlParser_Chrome rcp = new RevisionControlParser_Chrome();
		for(int i=2519; i<251728;i++) //for(int i=4; i<251728;i++)
		{
			rcp.setRevisionID(i);
			rcp.process();
			String logMessage = rcp.getLogMessage();
			String author = rcp.getAuthor();
			String timestamp = rcp.getTimeStamp();
			Timestamp timestamp2 = rcp.getTimeStamp2();
			String file_list = rcp.getFileList();
			System.out.println("i="+i+"");
			//System.out.println("Log Message : " + logMessage);
			//System.out.println("Auhtor : " + author);
			//System.out.println("Timestamp : " + timestamp);
			//System.out.println("Converted Timestamp2 : " + timestamp2);
			//System.out.println("File List :"+ file_list);
			insert_in_table(i,author, timestamp2,logMessage, file_list,connect);
            
			System.out.println("------------------------------");
		}
		
		try {
			connect.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private Timestamp getTimeStamp2() {
		// TODO Auto-generated method stub
		return timestamp2;
	}

	private static  void insert_in_table(int i, String author, Timestamp timestamp2,
			String logMessage, String file_list, Connection connect)
	{
	  String str = "insert into "+table+" values("+i+",'"+author+"','"+timestamp2+"','"+logMessage+"','"+ file_list+"')";
	  System.out.println(str);
	  Statement stmt = null;
	  try {
		stmt = connect.createStatement();
		stmt.execute(str);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		System.exit(0);
	}
		
	}

}